package org.sixtynine.stock.controller.front;

import javax.servlet.http.HttpServletRequest;

import org.sixtynine.stock.criteria.StockSearchCriteria;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.model.ColData;
import org.sixtynine.stock.model.GridData;
import org.sixtynine.stock.result.ShareResult;
import org.sixtynine.stock.service.StockManagerService;
import org.sixtynine.stock.service.file.FileDataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilanka
 * 
 */
@Controller
public class SnapShotController extends AbstractGridController {

	@Autowired
	private StockManagerService service;

	@Autowired
	private FileDataServiceImpl fileDataServiceImpl;

	@RequestMapping(value = "/snapshot/main")
	public ModelAndView showResults(
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@ModelAttribute StockSearchCriteria criteria,
			HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("/snapshot/main");

		String companyDocsLocation = fileDataServiceImpl
				.getCompanyDocLocation();

		criteria._setAddCompanyDocs(true);
		GridData<ShareResult> gridData = getGridData(criteria, page, pageSize,
				null, null, null, request);

		int startIndex = (page - 1) * pageSize;
		Page<ShareResult> pageData = new Page<ShareResult>(
				gridData.getRecords(), startIndex, gridData.getData().size(),
				pageSize, gridData.getData());

		modelAndView.addObject("data", pageData);
		modelAndView.addObject("companyDocDir", companyDocsLocation);

		return modelAndView;
	}

	@RequestMapping(value = "/snapshot/list")
	public ModelAndView searchResults(
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "code", required = false) String code,
			@RequestParam(value = "name", required = false) String name,
			HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("/snapshot/main");

		String companyDocsLocation = fileDataServiceImpl
				.getCompanyDocLocation();

		StockSearchCriteria criteria = new StockSearchCriteria();
		if (name != null && name != "") {
			criteria.setCompanyName(name);
		}

		if (code != null && code != "") {

			criteria.setTicker(code);
		}

		criteria._setAddCompanyDocs(true);
		GridData<ShareResult> gridData = getGridData(criteria, page, pageSize,
				null, null, null, request);

		int startIndex = (page - 1) * pageSize;
		Page<ShareResult> pageData = new Page<ShareResult>(
				gridData.getRecords(), startIndex, gridData.getData().size(),
				pageSize, gridData.getData());

		modelAndView.addObject("data", pageData);
		modelAndView.addObject("companyDocDir", companyDocsLocation);

		return modelAndView;
	}

	@Override
	public ColData getColData() {
		return null;
	}

}
